package com.simou.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simou.dao.SetmealMapper;
import com.simou.dto.SetmealDto;
import com.simou.entity.Category;
import com.simou.entity.Setmeal;
import com.simou.entity.SetmealDish;
import com.simou.service.CategoryService;
import com.simou.service.SetmealDishService;
import com.simou.service.SetmealService;
import com.simou.vo.SetmealVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Override
    public void saveWithSetmealDish(SetmealDto setmealDto) {

        // 将套餐保存到setmeal表中
        save(setmealDto);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        // 将套餐的菜品保存到setmealDish表中
        for (SetmealDish setmealDish : setmealDishList) {
            // 因为list是引用里面的元素，故只修改元素，列表对应元素里面的属性就会改变
            // 添加setmealId
            setmealDish.setSetmealId(String.valueOf(setmealDto.getId()));
        }
        setmealDishService.saveBatch(setmealDishList);

    }

    @Override
    public SetmealVo getByIdWithSetmealDishAndCategory(Long id) {
        // 查询setmeal表
        Setmeal setmeal = getById(id);

        // 查询setmealDish表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        // 查询Category表
        Category category = categoryService.getById(setmeal.getCategoryId());

        // 用setmealVo对象整合数据
        SetmealVo setmealVo = new SetmealVo();
        BeanUtils.copyProperties(setmeal,setmealVo);
        setmealVo.setSetmealDishes(setmealDishes);
        setmealVo.setCategoryName(category.getName());

        return setmealVo;
    }

    @Override
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        // 1.修改setmeal表
        updateById(setmealDto);

        // 2.修改setmealDish表
        // 先删除绑定该id的记录
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        // 插入新的记录
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId().toString());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void deleteWithSetmealDish(Long id) {
        // 删除setmealDish表的记录
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(queryWrapper);

        // 删除setmeal表的记录
        removeById(id);
    }
}
